package com.hibikiluler.javalearning.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hibikiluler.javalearning.entity.Comments;
import com.hibikiluler.javalearning.mapper.CommentMapper;
import com.hibikiluler.javalearning.service.CommentService;
import com.hibikiluler.javalearning.util.DateUtils;
import com.hibikiluler.javalearning.util.PageResult;
import com.hibikiluler.javalearning.util.Queryinfo;
import com.hibikiluler.javalearning.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public Result delete(Integer id) {
        if (id == null) {
            return Result.fail("请传递删除ID");
        }
        commentMapper.delete(id);
        return Result.success("删除成功");
    }

    @Override
    public Result insert(Comments comments) {
        comments.setSubmitTime(DateUtils.getDateTime());
        commentMapper.insert(comments);
        return Result.success("添加成功");
    }

    @Override
    public Result findPage(Queryinfo queryInfo) {
        log.info("开始数据分页-->页码{}, --->{}页数--->查询内容{}", queryInfo.getPagenumber(), queryInfo.getPagesize(), queryInfo.getQueryString());
        PageHelper.startPage(queryInfo.getPagenumber(), queryInfo.getPagesize());
        Page<Comments> comments = commentMapper.findPage(queryInfo.getQueryString());
        long total = comments.getTotal();
        List<Comments> result = comments.getResult();
        log.info("查询的总条数-->{}", total);
        log.info("分页列表-->{}", result);
        return PageResult.pageResult(total, result);
    }
}
